/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.share.mybatis.mapper;

import com.share.common.entity.Order;
import com.share.common.repository.OrderRepository;
import org.apache.ibatis.annotations.*;

import java.sql.SQLException;
import java.util.List;

@Mapper
public interface MybatisOrderRepository extends OrderRepository {
    @Override
    @Update("CREATE TABLE IF NOT EXISTS t_order (order_id BIGINT AUTO_INCREMENT, user_id INT NOT NULL, address_id BIGINT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id));")
    void createTableIfNotExists() throws SQLException;

    @Override
    @Update("DROP TABLE IF EXISTS t_order;")
    void dropTable() throws SQLException;

    @Override
    @Update("TRUNCATE TABLE t_order;")
    void truncateTable() throws SQLException;

    @Override
    @Insert("INSERT INTO t_order (user_id, address_id, status) VALUES (#{userId,jdbcType=INTEGER}, #{addressId,jdbcType=BIGINT}, #{status,jdbcType=VARCHAR});")
    Long insert(Order entity) throws SQLException;

    @Override
    @Delete("DELETE FROM t_order WHERE order_id = #{orderId,jdbcType=INTEGER};")
    void delete(Long primaryKey) throws SQLException;

    @Override
    @Results(id = "orderAll", value = {
            @Result(column="order_id", property = "orderId"),
            @Result(column="user_id", property="userId"),
            @Result(column="address_id", property="addressId"),
            @Result( column="status", property="status")
    })
    @Select("SELECT * FROM t_order;")
    List<Order> selectAll() throws SQLException;
}
